package com.aqiang.day0528_shop.main.view.activity

import android.content.ContentValues
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.contract.GoodsTypeContract
import com.aqiang.day0528_shop.main.entity.SearchGoodsEntity
import com.aqiang.day0528_shop.sql.CarSql
import com.aqiang.lib_core.common.SLog
import com.aqiang.lib_core.mvp.BaseActivity
import com.google.gson.Gson
import com.tencent.smtt.export.external.interfaces.JsResult
import com.tencent.smtt.sdk.ValueCallback
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import kotlinx.android.synthetic.main.activity_product_details.*
import org.jetbrains.anko.toast

class ProductDetailsActivity : BaseActivity<GoodsTypeContract.GoodsTypePresenter>() {
    private var data:String = ""
    private lateinit var carSql: CarSql
    override fun bindLatout(): Int {
        return R.layout.activity_product_details
    }

    override fun createPresenter() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        data = intent.getStringExtra("data")
        web_act_pro.settings.javaScriptEnabled = true
        web_act_pro.loadUrl("file:///android_asset/demo.html")
        web_act_pro.webViewClient = object : WebViewClient(){
            override fun onPageFinished(p0: WebView?, p1: String?) {
                super.onPageFinished(p0, p1)
                if(data != null){
                    web_act_pro.loadUrl("javascript:setData('${data}')")
                }
            }

            override fun onLoadResource(p0: WebView?, p1: String?) {
                super.onLoadResource(p0, p1)
            }
        }
        web_act_pro.webChromeClient =object :WebChromeClient(){
            override fun onJsAlert(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
                //toast(p1!!)
                return false
            }
        }
    }

    override fun initData() {
        carSql = CarSql.getInstance(this)

    }

    override fun initEvent() {
        bt_act_add_car.setOnClickListener {
            if(data != null){
                val searchGoodsEntity = Gson().fromJson(data, SearchGoodsEntity::class.java)
                carSql.use {
                    val contentValues = ContentValues()
                    contentValues.put("shoptitle",searchGoodsEntity.ShopTitle)
                    contentValues.put("tilte",searchGoodsEntity.Title)
                    contentValues.put("sellerid",searchGoodsEntity.SellerId)
                    contentValues.put("inclu",0)
                    contentValues.put("price",searchGoodsEntity.ReservePrice)
                    contentValues.put("img",searchGoodsEntity.PictUrl)
                    insert(CarSql.nameDb,null,contentValues)
                }
            }
        }
    }

}
